var yagnus=require('../npm/yagnus');
var ae=require('../doubleTests').ae;

module.exports = {
    setUp: function (callback) {
        callback();
    },
    tearDown: function (callback) {
        callback();
    },

    test_basic_get_set: function (test) {

		var s = org.yagnus.stats.initUnivariateSummaryStatistics();

		s.inc(1);
		s.inc(2);
		s.inc(3);

		test.equals(3,s.c,"\sum 1="+s.c);
		test.equals(6,s.s,"\sum s="+s.s);
		test.equals(14,s.ss,"\sum s*s="+s.ss);
		test.equals(36,s.sss,"\sum s*s*s="+s.sss);
		test.equals(98,s.ssss,"\sum s*s*s*s="+s.ssss);

		var r=s.calc();

		test.ok(ae(3,r.count),"checking count");
		test.ok(ae(6,r.sum),"checking sum");
		test.ok(ae(2,r.average),"checking average");

		test.ok(ae(1,r.minimum),"checking minimum");
		test.ok(ae(3,r.maximum),"checking maximum:"+r.maximum);

		test.ok(ae(0,r.skewness),"checking skewness");
		test.ok(ae(1.5,r.kurtosis),"checking kurtosis "+1.5+"=="+s.kurtosis);

		test.done();

	},


   test_unif_100_numbers: function (test) {
		var a=[0.72076448,0.63310624,0.65723163,0.24805087,0.02338843,0.86919149,0.68436800,0.09695604,0.76520313,0.03404326,0.05987335,0.07875776,0.42602662,0.73345851,0.80641551,0.57079023,0.85281996,0.37257507,0.42021476,0.32660935,0.10805355,0.51855493,0.81972207,0.09652919,0.22619716,0.15831484,0.06851760,0.75925054,0.93765613,0.66313939,0.86523702,0.61556625,0.30834328,0.82254970,0.64647114,0.66104343,0.98877485,0.31665421,0.19275225,0.68635881,0.80133036,0.94163136,0.60093148,0.22290593,0.37415254,0.89918005,0.24404935,0.51486351,0.59921544,0.46089539,0.51369847,0.16682968,0.39843101,0.28366509,0.87126228,0.98142633,0.22446443,0.04140600,0.33821650,0.63554007,0.51348293,0.35730615,0.24666949,0.31806643,0.55374195,0.66766706,0.97375211,0.54006306,0.86734398,0.47491026,0.17251286,0.21847882,0.20189402,0.85706769,0.61385071,0.46991185,0.31056833,0.99294152,0.34166058,0.53993132,0.29115742,0.18010233,0.40525460,0.39892456,0.37626519,0.30580674,0.50706515,0.12971026,0.20621256,0.11576002,0.32024904,0.55125223,0.07004954,0.87202704,0.18468871,0.40370124,0.30604595,0.78234751,0.04404918,0.92814878];
		var s = org.yagnus.stats.initUnivariateSummaryStatistics();
		for (i in a)s.inc(a[i]);
		var r=s.calc();

		test.ok(ae(100,r.count),"checking count");
		test.ok(ae(47.56427,r.sum),"checking sum");
		test.ok(ae(0.4756427,r.average),"checking average");

		test.ok(ae(0.02338843,r.minimum),"checking minimum");
		test.ok(ae(0.9929415,r.maximum),"checking maximum");

		test.ok(ae(0.19461,r.skewness, 3e-3),"checking skewness "+0.19461+"=="+s.skewness);
		test.ok(ae(1.883162,r.kurtosis),"checking kurtosis "+1.883162+"=="+s.kurtosis);

		test.done();

	},


	test_norm_250_numbers: function (test) {
		var a=[-1.824749186,1.324528470,-0.056620330,-0.490958853,0.917472924,-0.185338048,0.338681491,-0.981388455,-1.430090253,-0.944528836,0.938074990,0.843994255,-0.560034732,-0.596564569,-2.049420895,0.231846929,-1.623648500,-0.949190708,-0.592338464,0.210576553,0.097825670,-0.902205929,0.374074161,-1.366517839,-0.881293747,-0.394833951,1.821448642,-0.897939977,2.209990378,1.099563984,0.289716612,0.324517761,-0.329760623,-0.271429808,-0.172629348,1.120490767,1.385390503,-1.945937272,0.817231063,-0.354167311,-0.880201829,0.137056488,-0.121560795,0.557673585,0.283960834,-0.043511104,-2.358492789,0.741253167,-0.614385046,0.365476462,1.377959819,-0.745092912,0.615010807,1.396524792,-0.263727849,-1.215575804,1.469747608,0.661054199,-2.430550599,1.498956218,2.425444104,-0.765581622,-2.797826304,-1.671296975,0.213968428,0.439041143,0.496811897,0.065380791,-0.808143449,-0.537797683,-0.862890771,-0.762414305,-0.645569469,-1.146344468,0.124354986,0.270421773,1.503565454,0.260415102,0.841664894,0.001836529,1.532899936,-0.692041085,0.039994056,0.010157809,-2.355033025,-1.443230206,1.700232780,0.435082213,0.270802652,-0.851691820,0.566652957,0.650009402,-1.909313716,0.172961654,0.870758960,-0.611177668,0.520682567,0.860248747,-0.331717379,-0.362526880,1.257138253,-1.770846636,-0.541661239,0.013280735,0.007393758,-0.033639197,-3.178964647,1.979962525,0.477198797,-0.117945500,1.031795028,-0.893789592,1.823989482,-0.041232884,0.078466807,1.274505469,0.792754962,1.824612258,-2.403123643,-0.144234459,-0.060806263,1.017163364,-0.884345993,0.598116849,1.766695509,-0.463755229,0.861746987,-1.041374422,-0.331812859,-0.773244267,1.948448559,-2.322983564,0.293292711,1.675204177,0.242906453,1.316681259,3.314103357,0.158518154,0.537948529,-0.440382920,-1.665386297,-0.248028614,0.463650305,0.595123035,2.034602825,0.926183325,-0.076965530,-0.553522951,0.479530503,-0.552309070,-1.359509295,1.056622877,-1.536832406,-0.013322993,0.308528279,1.594155608,-0.463172025,0.967386152,-0.024514895,-0.629822420,1.066490794,0.658458525,1.395763302,-0.369771585,2.000942440,-1.146983753,-0.503412468,2.007100932,0.921071699,0.162363921,0.645502957,0.354060417,2.324013326,1.135090952,-1.826175886,0.191363754,0.599444815,1.420375292,0.502195725,-1.538507184,0.244278648,1.086293664,-0.360601981,-0.708704818,0.599412074,-1.006852818,-2.064890973,0.625752283,-0.260698229,-0.139341456,-0.874049720,0.072453005,1.383053614,0.420838998,0.710000079,-1.929031410,0.146000188,-0.774598611,-0.291555984,-0.217168557,-1.719218422,0.529791853,-0.092735083,1.275076448,-0.602057728,-0.430541414,-1.734228521,0.566806793,1.163490555,-1.301418159,-0.023200127,-1.930179123,0.773118988,0.285181108,1.009144548,-0.111785296,-0.564447589,2.437269103,1.132289805,0.120625299,0.299995005,-0.062475097,-0.164257716,0.729356333,-1.190645443,-0.793720568,-0.572179292,-1.117582077,1.504526145,-0.213223013,-1.855088309,0.572066653,-0.531358262,0.073250615,-0.998743163,-0.459210206,-0.922294814,0.424456433,0.516927165,0.438536452,-0.700053192,0.689520354,1.990809549,0.280351268,-0.224510609,0.092137220,0.147922334,0.486182678,-0.013097223,1.215183465];
		var s = org.yagnus.stats.initUnivariateSummaryStatistics();
		for (i in a)s.inc(a[i]);
		var r=s.calc();

		test.ok(ae(250,r.count),"checking count");
		test.ok(ae(0.03172049, r.average),"checking average");
		test.ok(ae(7.930124,   r.sum),"checking sum");

		test.ok(ae(-3.178965, r.minimum),"checking minimum");
		test.ok(ae(3.314103,  r.maximum),"checking maximum");

		test.ok(ae(-0.138297, r.skewness,2e-3),"checking skewness "+r.skewness);
		test.ok(ae(3.090568,r.kurtosis),"checking kurtosis ");

		test.done();
	},


	test_norm_250_updates: function (test) {
		var a=[-1.824749186,1.324528470,-0.056620330,-0.490958853,0.917472924,-0.185338048,0.338681491,-0.981388455,-1.430090253,-0.944528836,0.938074990,0.843994255,-0.560034732,-0.596564569,-2.049420895,0.231846929,-1.623648500,-0.949190708,-0.592338464,0.210576553,0.097825670,-0.902205929,0.374074161,-1.366517839,-0.881293747,-0.394833951,1.821448642,-0.897939977,2.209990378,1.099563984,0.289716612,0.324517761,-0.329760623,-0.271429808,-0.172629348,1.120490767,1.385390503,-1.945937272,0.817231063,-0.354167311,-0.880201829,0.137056488,-0.121560795,0.557673585,0.283960834,-0.043511104,-2.358492789,0.741253167,-0.614385046,0.365476462,1.377959819,-0.745092912,0.615010807,1.396524792,-0.263727849,-1.215575804,1.469747608,0.661054199,-2.430550599,1.498956218,2.425444104,-0.765581622,-2.797826304,-1.671296975,0.213968428,0.439041143,0.496811897,0.065380791,-0.808143449,-0.537797683,-0.862890771,-0.762414305,-0.645569469,-1.146344468,0.124354986,0.270421773,1.503565454,0.260415102,0.841664894,0.001836529,1.532899936,-0.692041085,0.039994056,0.010157809,-2.355033025,-1.443230206,1.700232780,0.435082213,0.270802652,-0.851691820,0.566652957,0.650009402,-1.909313716,0.172961654,0.870758960,-0.611177668,0.520682567,0.860248747,-0.331717379,-0.362526880,1.257138253,-1.770846636,-0.541661239,0.013280735,0.007393758,-0.033639197,-3.178964647,1.979962525,0.477198797,-0.117945500,1.031795028,-0.893789592,1.823989482,-0.041232884,0.078466807,1.274505469,0.792754962,1.824612258,-2.403123643,-0.144234459,-0.060806263,1.017163364,-0.884345993,0.598116849,1.766695509,-0.463755229,0.861746987,-1.041374422,-0.331812859,-0.773244267,1.948448559,-2.322983564,0.293292711,1.675204177,0.242906453,1.316681259,3.314103357,0.158518154,0.537948529,-0.440382920,-1.665386297,-0.248028614,0.463650305,0.595123035,2.034602825,0.926183325,-0.076965530,-0.553522951,0.479530503,-0.552309070,-1.359509295,1.056622877,-1.536832406,-0.013322993,0.308528279,1.594155608,-0.463172025,0.967386152,-0.024514895,-0.629822420,1.066490794,0.658458525,1.395763302,-0.369771585,2.000942440,-1.146983753,-0.503412468,2.007100932,0.921071699,0.162363921,0.645502957,0.354060417,2.324013326,1.135090952,-1.826175886,0.191363754,0.599444815,1.420375292,0.502195725,-1.538507184,0.244278648,1.086293664,-0.360601981,-0.708704818,0.599412074,-1.006852818,-2.064890973,0.625752283,-0.260698229,-0.139341456,-0.874049720,0.072453005,1.383053614,0.420838998,0.710000079,-1.929031410,0.146000188,-0.774598611,-0.291555984,-0.217168557,-1.719218422,0.529791853,-0.092735083,1.275076448,-0.602057728,-0.430541414,-1.734228521,0.566806793,1.163490555,-1.301418159,-0.023200127,-1.930179123,0.773118988,0.285181108,1.009144548,-0.111785296,-0.564447589,2.437269103,1.132289805,0.120625299,0.299995005,-0.062475097,-0.164257716,0.729356333,-1.190645443,-0.793720568,-0.572179292,-1.117582077,1.504526145,-0.213223013,-1.855088309,0.572066653,-0.531358262,0.073250615,-0.998743163,-0.459210206,-0.922294814,0.424456433,0.516927165,0.438536452,-0.700053192,0.689520354,1.990809549,0.280351268,-0.224510609,0.092137220,0.147922334,0.486182678,-0.013097223,1.215183465];

		var ss=[];
		for(var i=0;i<5;++i)ss.push(org.yagnus.stats.initUnivariateSummaryStatistics());
		for(var i=0;i<a.length;++i)ss[i%5].o(a[i]);
		var s=org.yagnus.stats.initUnivariateSummaryStatistics();
		for(var i=0;i<5;++i)s.update(ss[i]);
		var r=s.calc();

		test.ok(ae(250,r.count),"checking count");
		test.ok(ae(0.03172049, r.average),"checking average");
		test.ok(ae(7.930124,   r.sum),"checking sum");

		test.ok(ae(-3.178965, r.minimum),"checking minimum");
		test.ok(ae(3.314103,  r.maximum),"checking maximum");

		test.ok(ae(-0.138297, r.skewness,2e-3),"checking skewness "+r.skewness);
		test.ok(ae(3.090568,r.kurtosis),"checking kurtosis ");

		test.done();
	},
	test_norm_250_update_via_incs: function (test) {
		var a=[-1.824749186,1.324528470,-0.056620330,-0.490958853,0.917472924,-0.185338048,0.338681491,-0.981388455,-1.430090253,-0.944528836,0.938074990,0.843994255,-0.560034732,-0.596564569,-2.049420895,0.231846929,-1.623648500,-0.949190708,-0.592338464,0.210576553,0.097825670,-0.902205929,0.374074161,-1.366517839,-0.881293747,-0.394833951,1.821448642,-0.897939977,2.209990378,1.099563984,0.289716612,0.324517761,-0.329760623,-0.271429808,-0.172629348,1.120490767,1.385390503,-1.945937272,0.817231063,-0.354167311,-0.880201829,0.137056488,-0.121560795,0.557673585,0.283960834,-0.043511104,-2.358492789,0.741253167,-0.614385046,0.365476462,1.377959819,-0.745092912,0.615010807,1.396524792,-0.263727849,-1.215575804,1.469747608,0.661054199,-2.430550599,1.498956218,2.425444104,-0.765581622,-2.797826304,-1.671296975,0.213968428,0.439041143,0.496811897,0.065380791,-0.808143449,-0.537797683,-0.862890771,-0.762414305,-0.645569469,-1.146344468,0.124354986,0.270421773,1.503565454,0.260415102,0.841664894,0.001836529,1.532899936,-0.692041085,0.039994056,0.010157809,-2.355033025,-1.443230206,1.700232780,0.435082213,0.270802652,-0.851691820,0.566652957,0.650009402,-1.909313716,0.172961654,0.870758960,-0.611177668,0.520682567,0.860248747,-0.331717379,-0.362526880,1.257138253,-1.770846636,-0.541661239,0.013280735,0.007393758,-0.033639197,-3.178964647,1.979962525,0.477198797,-0.117945500,1.031795028,-0.893789592,1.823989482,-0.041232884,0.078466807,1.274505469,0.792754962,1.824612258,-2.403123643,-0.144234459,-0.060806263,1.017163364,-0.884345993,0.598116849,1.766695509,-0.463755229,0.861746987,-1.041374422,-0.331812859,-0.773244267,1.948448559,-2.322983564,0.293292711,1.675204177,0.242906453,1.316681259,3.314103357,0.158518154,0.537948529,-0.440382920,-1.665386297,-0.248028614,0.463650305,0.595123035,2.034602825,0.926183325,-0.076965530,-0.553522951,0.479530503,-0.552309070,-1.359509295,1.056622877,-1.536832406,-0.013322993,0.308528279,1.594155608,-0.463172025,0.967386152,-0.024514895,-0.629822420,1.066490794,0.658458525,1.395763302,-0.369771585,2.000942440,-1.146983753,-0.503412468,2.007100932,0.921071699,0.162363921,0.645502957,0.354060417,2.324013326,1.135090952,-1.826175886,0.191363754,0.599444815,1.420375292,0.502195725,-1.538507184,0.244278648,1.086293664,-0.360601981,-0.708704818,0.599412074,-1.006852818,-2.064890973,0.625752283,-0.260698229,-0.139341456,-0.874049720,0.072453005,1.383053614,0.420838998,0.710000079,-1.929031410,0.146000188,-0.774598611,-0.291555984,-0.217168557,-1.719218422,0.529791853,-0.092735083,1.275076448,-0.602057728,-0.430541414,-1.734228521,0.566806793,1.163490555,-1.301418159,-0.023200127,-1.930179123,0.773118988,0.285181108,1.009144548,-0.111785296,-0.564447589,2.437269103,1.132289805,0.120625299,0.299995005,-0.062475097,-0.164257716,0.729356333,-1.190645443,-0.793720568,-0.572179292,-1.117582077,1.504526145,-0.213223013,-1.855088309,0.572066653,-0.531358262,0.073250615,-0.998743163,-0.459210206,-0.922294814,0.424456433,0.516927165,0.438536452,-0.700053192,0.689520354,1.990809549,0.280351268,-0.224510609,0.092137220,0.147922334,0.486182678,-0.013097223,1.215183465];

		var ss=[];
		for(var i=0;i<5;++i)ss.push(org.yagnus.stats.initUnivariateSummaryStatistics());
		for(var i=0;i<a.length;++i)ss[i%5].o(a[i]);
		var s=org.yagnus.stats.initUnivariateSummaryStatistics();
		for(var i=0;i<5;++i)s.inc(ss[i]);
		var r=s.calc();

		test.ok(ae(250,r.count),"checking count");
		test.ok(ae(0.03172049, r.average),"checking average");
		test.ok(ae(7.930124,   r.sum),"checking sum");

		test.ok(ae(-3.178965, r.minimum),"checking minimum");
		test.ok(ae(3.314103,  r.maximum),"checking maximum");

		test.ok(ae(-0.138297, r.skewness,2e-3),"checking skewness "+r.skewness);
		test.ok(ae(3.090568,r.kurtosis),"checking kurtosis ");

		test.done();
	},
	test_observation_order:function (test) {
		var a=[];
		for(var i=0;i<1000;++i)a.push(i);

		var s1=org.yagnus.stats.initUnivariateSummaryStatistics();
		for(var i=0;i<a.length;++i)s1.inc(a[i]);

		var s2=org.yagnus.stats.initUnivariateSummaryStatistics();
		for(var i=a.length-1;i>-1;--i)s2.o(a[i]);

		var r1=s1.calc();
		var r2=s2.calc();
		test.deepEqual(r1,r2,"Summary stats should be same irrespective of the order in which we added the data.");

		test.done();
	},

	test_copy_constructor:function (test) {
		var s1=org.yagnus.stats.initUnivariateSummaryStatistics();
		for(var i=0;i<1000;++i)s1.o(i);
		for(var i=1234;i>-5 ;--i)s1.o(i);
		var s2=org.yagnus.stats.initUnivariateSummaryStatistics(s1);
		var c1=s1.calc();
		var c2=s2.calc();
		test.ok(ae(c1.average,c2.average,1e-5),"sanity check");
		test.ok(org.yagnus.utils.equalsIgnoreMember(c1,c2), "Check that the whole result objects are equal");
		test.done();
	},
	test_short_hand:function (test) {
		test.equals(org.yagnus.stats.initUnivariateSummaryStatistics, oy.UVar, "checking that the oy shorthand works.");
		test.equals(org.yagnus.stats.initUnivariateSummaryStatistics, oys.UVar, "checking that the oy shorthand works.");
		test.done();
	}
}
